<template>
  <div>
    <div class="head">
      <van-nav-bar>
        <template #left>
          <div class="logo">
            <img src="../assets/images/home_active.png" alt="" />
            <span class="logo-title">Luckin Coffee</span>
          </div>
        </template>
        <template #right>
          <div class="goBefore" @click="goLook">先逛一逛</div>
        </template>
      </van-nav-bar>
    </div>
    <div class="login-box">
      <h1 class="login-title">找回密码！</h1>
      <p class="login-subTitle">Find password</p>
      <van-form @submit="onSubmit">
        <van-field
          v-model="phone"
          name="手机号"
          label="手机号"
          placeholder="手机号"
          :rules="[
            {
              required: true,
              message: '请填写正确手机号',
              pattern: /^1[3-9][0-9]{9}$/,
            },
          ]"
        />
        <van-field
          v-model="password"
          :type="isHidden ? 'text' : 'password'"
          name="密码"
          label="密码"
          placeholder="密码"
          :right-icon="isHidden ? 'eye-o' : 'closed-eye'"
          @click-right-icon="isHidden = !isHidden"
          :rules="[
            {
              required: true,
              message: '必须以字母开头,6-16位字母数字下划线组合',
              pattern: /^[A-Za-z]\w{5,15}$/,
            },
          ]"
        />
        <div class="login-tip">
          <span @click="goLogin">已有账号,立即登录</span>
        </div>
        <div style="margin: 16px">
          <van-button
            round
            block
            type="info"
            native-type="submit"
            color="rgb(12,52,186)"
            >提交</van-button
          >
        </div>
      </van-form>
      <div style="margin: 16px">
        <van-button
          round
          block
          type="info"
          native-type="submit"
          color="#777"
          plain
          @click="back"
          >返回</van-button
        >
      </div>
    </div>
  </div>
</template>

<script>
import { Toast } from "vant";
export default {
  name: "Reset",
  data() {
    return {
      appkey: "U2FsdGVkX19WSQ59Cg+Fj9jNZPxRC5y0xB1iV06BeNA=",
      phone: "",
      password: "",
      isHidden: false,
    };
  },
  methods: {
    onSubmit() {
      let phone = this.phone;
      let password = this.password;
      let appkey = this.appkey;

      let data = `appkey=${appkey}&phone=${phone}&password=${password}`;
      this.axios({
        method: "post",
        url: "http://www.kangliuyong.com:10002/retrievePassword",
        data,
      })
        .then((res) => {
          
          if (res.data.code === "L001") {
            Toast.success("重置密码成功");
            this.$router.push({ name: "Login" });
          } else {
            Toast("找回密码失败,请输入正确手机号和密码");
          }
        })
        .catch((err) => {
          
        });
    },
    back() {
      this.$router.go(-1);
    },
    goLogin() {
      this.$router.push({ name: "Login" });
    },
    goLook() {
      this.$router.push({ name: "Home" });
    },
  },
};
</script>

<style lang="less" scoped>
.head {
  .logo {
    display: flex;
    align-items: center;
    img {
      height: 36px;
    }
    .logo-title {
      font-size: 16px;
      font-weight: bold;
    }
  }
  .goBefore {
    color: rgb(12, 52, 186);
  }
}
.login-box {
  padding: 0 20px;
  margin-top: 30%;
  .login-title {
    color: #666;
  }
  .login-subTitle {
    color: #666;
    font-size: 16px;
  }
  .login-tip {
    text-align: right;
    font-size: 14px;
    line-height: 30px;
    margin-top: 10px;
    margin-bottom: 40px;
    padding-right: 10px;
  }
  .popup-title {
    color: #666;
    font-size: 24px;
    padding: 10px;
    font-weight: bold;
  }
}
/deep/.van-field__label {
  margin: 0;
}
</style>